package com.xiaoxin.controller;


import cn.hutool.Hutool;
import cn.hutool.crypto.SecureUtil;
import com.xiaoxin.common.Utils;
import com.xiaoxin.common.dto.ResponseBody;
import com.xiaoxin.entity.SysUser;
import com.xiaoxin.entity.vo.UserVo;
import com.xiaoxin.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.security.SecurityUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author TongRui
 * @since 2021-11-28
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class SysUserController {

    @Resource
    private ISysUserService userService;

    @PostMapping
    public ResponseBody save(@RequestBody UserVo userVo){
        var user = new SysUser();
        BeanUtils.copyProperties(userVo, user);
        user.setSalt(Utils.randomStr(5));
        user.setJwtSalt(Utils.randomStr(5));
        user.setPassword(SecureUtil.md5(user.getPassword()+user.getSalt()));
        log.info("创建新用户成功{}", user);
        return ResponseBody.ok(userService.save(user));
    }
}

